<template>
  <el-header class="me-area">
    <el-row class="me-header">
      <el-col :span="4" class="me-header-left">
        <router-link to="/index" class="me-title">
          <img class="logpng" src="../assets/anyi.png" />
        </router-link>
      </el-col>

      <el-col v-if="1" :span="14">
        <el-menu
          :router="true"
          menu-trigger="click"
          active-text-color="#5FB878"
          :default-active="activeIndex"
          mode="horizontal"
        >
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/user/me">个人简介</el-menu-item>
          <el-col :span="10" :offset="15" class="addArticel">
            <el-menu-item v-if="hasLogin"
              ><i class="el-icon-edit"></i>
              <router-link :to="{ name: 'BlogAdd' }"
                >写文章</router-link
              ></el-menu-item
            >
          </el-col>
        </el-menu>
      </el-col>

      <el-col :span="6">
        <el-menu
          :router="true"
          menu-trigger="click"
          mode="horizontal"
          active-text-color="#5FB878"
        >
          <template v-if="!hasLogin">
            <el-menu-item index="/login" class="login">
              <el-button type="text">登录</el-button>
            </el-menu-item>
          </template>

          <template v-else>
            <el-submenu index>
              <template slot="title">
                <img class="me-header-picture" :src="user.avatar" />
              </template>
              <el-menu-item index @click="logout"
                ><i class="el-icon-back"></i>退出</el-menu-item
              >
            </el-submenu>
          </template>
        </el-menu>
      </el-col>
    </el-row>
  </el-header>
</template>

<script>
export default {
  name: 'BaseHeader.vue',
  data() {
    return {
      activeIndex: '/',
      hasLogin: false,
      user: {
        username: '安逸i',
        avatar: 'http://www.ease.center/images/default.jpg',
      },
    }
  },
  created() {
    if (this.$store.getters.getUser.username) {
      let userinfo = this.$store.getters.getUser
      this.user.username = userinfo.username
      this.user.avatar = userinfo.avator
      this.hasLogin = true
    }
    if (sessionStorage.getItem('userinfo') != null) {
      let userinfo = JSON.parse(sessionStorage.getItem('userinfo'))
      this.user.username = userinfo.username
      this.user.avatar = userinfo.avatar
      this.hasLogin = true
    }
  },
  methods: {
    logout() {
      const _this = this
      this.$axios
        .get('/user/layout', {
          headers: {
            Authorization: localStorage.getItem('token'),
          },
        })
        .then((result) => {
          _this.$store.commit('REMOVE_INFO')
          _this.$router.push('/login')
        })
        .catch((err) => {})
    },
  },
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
}
.addArticel {
  width: 70px !important;
}
.el-menu--horizontal {
  margin-left: 20px;
}
.el-header {
  position: fixed;
  z-index: 1024;
  min-width: 100%;
  box-shadow: 0 2px 3px hsla(0, 0%, 7%, 0.1), 0 0 0 1px hsla(0, 0%, 7%, 0.1);
}
.login {
  margin-left: 80px !important;
}

.me-title {
  margin-top: 10px;
  font-size: 24px;
}
.me-header-left {
  margin-top: 10px;
}
.me-title img {
  max-height: 2.4rem;
  max-width: 100%;
}
.el-col-offset-4 {
  float: right !important;
}
.me-header-picture {
  width: 36px;
  height: 36px;
  border: 1px solid #ddd;
  border-radius: 50%;
  vertical-align: middle;
  background-color: #5fb878;
}
</style>
